$('.con-wp2').on('click', '.jian,.jia,.ppdel', function () {
    // console.log($('.b-r-four input').val());
    // console.log($(event.target));
    var index = $(this).parent().children('input').val()
    if (index > 1) {
        if ($(event.target).is($('.jian'))) {
            $(this).parent().children('input').val(--index)
        }
    }
    if (index < 5) {
        if ($(event.target).is($('.jia'))) {
            $(this).parent().children('input').val(++index)

        }
    }
    if ($(event.target).is($('.ppdel'))) {
        $.ajax({
            url: './php/del.php',
            type: 'GET',
            dataType:'json',
            data: {
                id: $('.pid').text()
            },
            success: function (data) {
                if(data['code']==1){
                    showAA()
                }else{
                    alert('出错了')
                }
            }
        })
    }
})
$('.check-col>label').attr('res', 1)
$('.check-col2>.checkbox').attr('res', 1)
$('.check-col>label').click(function () {
    var i = $('.check-col>label').attr('res')
    console.log();
    // background-position: 0 -28px;
    if (i == 1) {
        $('.check-col>label').attr('res', 0)
        $('.check-col2>.checkbox').attr('res', 0)
        $(this).children('.checkbox').css('background-position', '0 -28px')
        $('.check-col2>.checkbox').css('background-position', '0 -28px')
        $('.sum>p:eq(0)>em').text($('.check-col2>.checkbox').length)
    } else {
        $('.check-col>label').attr('res', 1)
        $('.check-col2>.checkbox').attr('res', 1)
        $(this).children('.checkbox').css('background-position', '0 0')
        $('.check-col2>.checkbox').css('background-position', '0 0')
        $('.sum>p:eq(0)>em').text(0)
    }

})
$('.check-col2>.checkbox').click(function () {
    var i = $(this).attr('res')
    var inum = 0;
    if (i == 1) {
        $(this).attr('res', 0)
        $(this).css('background-position', '0 -28px')

    } else {
        $(this).attr('res', 1)
        $(this).css('background-position', '0 0')
    }

    $('.check-col2>.checkbox').each(function (index, item) {
        if ($(item).attr('res') == 0) {
            inum++;
        }
    })
    $('.sum>p:eq(0)>em').text(inum)
})
$('.con-wp').hide()
$('.con-wp2').hide()
showAA()
function showAA() {
    let uname = getCookie('uname');
    if (uname == undefined) {
        alert('请先登录')
        window.location.href = './login.html'
    } else {
        $.ajax({
            url: './php/showlist.php',
            data: `uname=${uname}`,
            dataType: 'json',
            success: function (data) {
                if (data['code'] == 1) {
                    $('.con-wp').hide()
                    $('.con-wp2').show()
                    data['data'].forEach(function (item) {
                        $('.cart-wrap').before(`
                    <div class="shop-wrap">
                    <span class="pid" style="display:none;">${item['product_id']}</span>
                    <table>
                        <tr>
                            <td class="check-col2"><i class="checkbox"></i></td>
                            <td class="pic-col"><div class="pic"><img src="${item['product_img']}" alt=""></div></td>
                            <td class="goods-col"><a href="#">${item['product_name']}</a></td>
                            <td class="price-col" style="color: #666;">${item['product_price']}</td>
                            <td><span class="num-box">
                                <a href="javascript:;" class="jian">-</a>
                                <input type="number" min="1" title="请输入购买量" readonly="readonly" value="${item['product_num']}" class="prod-num">
                                <a href="javascript:;" class="jia">+</a>
                            </span></td>
                            <td>0.00</td>
                            <td>4799.00</td>
                            <td><a href="javascript:;" class="ppdel">删除</a></td>
                        </tr>
                    </table>
                </div>
                    `)
                    })
                } else {
                    $('.con-wp').show()
                    $('.con-wp2').hide()
                }
            }
        })
    }

}

$('.wp-b>button').click(function(){
    window.location.href = './index.html'
})